<?php

namespace App\Http\Controllers\Admin\Customer;

use App\Exceptions\AppException;
use App\Exports\BigBagExport;
use App\Exports\BigBagManifestExport;
use App\Http\Controllers\Admin\CallBack;
use App\Http\Controllers\Controller;
use App\Models\customer\ResourceModel;
use App\Models\lazada\ApiModel;
use App\Models\lazada\LogModel;
use App\Models\OrderModel;
use App\Models\TakeOrderModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\lazada\NodeModel;
use App\Models\lazada\NodeRecordModel;
use App\Models\lazada\BigBagModel;

class BigBag extends Controller
{
    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $bigBagID = $request->input('bigBagID', '');
        $goods_attr = $request->input('bigBagType', '');
        $des_country_id = $request->input('des_country_id', '');
        $warehouse_id = $request->input('warehouse_id', '');
        $is_error = $request->input('is_error', '');
        $is_lack = $request->input('is_lack', '');
        $system_node_id = $request->input('node_id', '');
        $is_update = $request->input('is_update', '');
        $customer_id = $request->input('customer_id', '');
        $create_start = $request->input('startTime', '');
        $create_end = $request->input('endTime', '');

        $query = BigBagModel::query()->with(['customer', 'destination', 'take_order', 'system_node', 'platform_node', 'small']);

        if($bigBagID){
            $bigBagID = explode(',', $bigBagID);
        }
        if($bigBagID){
            $query->whereIn('bigBagID', $bigBagID);
        }

        if($customer_id){
            $query->where('customer_id', $customer_id);
        }

        if($goods_attr){
            $query->where('cargoType', $goods_attr);
        }

        if($des_country_id){
            $query->where('des_country_id', $des_country_id);
        }

        if($warehouse_id){
            $query->where('warehouse_id', $warehouse_id);
        }

        if($is_error != ''){
            $query->where('is_error', $is_error);
        }

        if($is_lack != ''){
            $query->where('is_lack', $is_lack);
        }

        if($system_node_id){
            $query->whereIn('system_node_id', $system_node_id);
        }

        if($is_update != ''){
            if($is_update == 1){
                $query->whereColumn('system_node_id', '=', 'platform_node_id');
            }else{
                $query->whereColumn('system_node_id', '<>', 'platform_node_id');
            }
        }

        if($create_start && $create_end){
            $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $total_package = $query->sum('parcelQty');
        $total_weight = $query->sum('bigBagWeight');
        $total_weight = round($total_weight / 1000, 2);
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        //资源代码
//        $resource = ResourceModel::query()->pluck('name', 'code')->toArray();

        $list = $items->map(function($item){
            $goods_value = $item->small->sum('price') ?? 0;
            $goods_value = $goods_value ? round($goods_value / 1000, 2) : 0;
            $res = [
                'id' => $item->id,
                'customer_name' => $item->customer->name,
                'bigBagID' => $item->bigBagID,
                'des_country_name' => $item->toLocation,
                'take_order_sn' => $item->take_order->sn ?? '',
                'goods_attr' => OrderModel::GOODS_ATTR_MAPS[$item->cargoType] ?? '',
//                'con_warehouse' => '',
//                'delivery_provider' => '',
//                'export_agent' => '',
                'goods_value' => $goods_value,
                'grossWeight' => round($item->bigBagWeight / 1000, 2),
                'parcelQty' => $item->parcelQty,
                'system_node' => $item->system_node->name_cn ?? '',
                'platform_node' => $item->platform_node->name_cn ?? '',
                'is_update' => $item->system_node_id == $item->platform_node_id ? '已更新' : '未更新',
                'created_at' => $item->created_at->toDateTimeString(),
                'remarks' => $item->remarks,
                'is_error' => $item->is_error,
                'is_lack' => $item->is_lack
            ];
            return $res;
        });

        $statistic = [
            'total' => $total,
            'total_package' => $total_package,
            'total_weight' => $total_weight,
        ];

        return $this->success(['list' => $list, 'total' => $total, 'statistic' => $statistic]);
    }

    public function remarks(Request $request, $id)
    {
        $remarks = $request->input('remarks', '');

        if(!$remarks){
            return $this->error('备注不能为空');
        }

        $find = BigBagModel::find($id);
        if(!$find){
            return $this->error('大包不存在');
        }

        $find->remarks = $remarks;
        $find->save();

        return $this->success();
    }

    /**
     * Notes:标记错误数据
     * User: xiangzi
     * Date: 2024/10/29
     * Time: 下午3:14
     * @return \Illuminate\Http\JsonResponse
     */
    public function mark_error(Request $request)
    {
        $id = $request->input('id', []);
        if(!$id){
            return $this->error('大包id不能为空');
        }

        BigBagModel::whereIn('id', $id)->update(['is_error' => 1]);
        return $this->success();
    }

    /**
     * Notes:标记缺失数据
     * User: xiangzi
     * Date: 2024/10/29
     * Time: 下午3:15
     * @return \Illuminate\Http\JsonResponse
     */
    public function mark_lack(Request $request)
    {
        $id = $request->input('id', []);

        if(!$id){
            return $this->error('大包id不能为空');
        }

        BigBagModel::whereIn('id', $id)->update(['is_lack' => 1]);
        return $this->success();
    }

    /** 标记正常数据
     * Notes:
     * User: xiangzi
     * Date: 2024/10/29
     * Time: 下午3:16
     * @return \Illuminate\Http\JsonResponse
     */
    public function mark_normal(Request $request)
    {
        $id = $request->input('id', []);

        if(!$id){
            return $this->error('大包id不能为空');
        }

        BigBagModel::whereIn('id', $id)->update(['is_lack' => 0, 'is_error' => 0]);
        return $this->success();
    }

    /**更新本地系统节点
     * Notes:
     * User: xiangzi
     * Date: 2024/11/1
     * Time: 上午10:38
     * @return \Illuminate\Http\JsonResponse
     */
    public function update_system_node(Request $request)
    {
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'id' => 'required|array',
            'node_id' => 'required',
            'datetime' => 'required'
        ], [
            'id.required' => 'id不能为空',
            'node_id.required' => '请选择节点',
            'datetime.required' => '请填写时间'
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $ids = $params['id'];

        DB::beginTransaction();
        try{
            foreach ($ids as $id){
                $find = BigBagModel::find($id);

                if(!$find){
                    return $this->error('大包不存在');
                }

                if($find->system_node_id != $find->platform_node_id){
                    return $this->error('大包【'.$find->bigBagID.'】还未更新');
                }

                $find->system_node_id = $params['node_id'];
                $find->save();

                //更新小包
                \App\Models\lazada\OrderModel::where('bigBagID',$find->bigBagID)->update(['system_node_id'=>$params['node_id']]);

                $record = new NodeRecordModel();
                $record->type = 1;
                $record->customer_id = $find->customer_id;
                $record->node_id = $params['node_id'];
                $record->bigbag_id = $find->id;
                $record->bigBagID = $find->bigBagID;
                $record->datetime = $params['datetime'];
                $record->record_type = 1;
                $record->created_admin_id = $auth->id;
                $record->save();
            }

            DB::commit();

        }catch (AppException $e){
            DB::rollBack();
            return $this->error($e->getMessage());
        }

        return $this->success();
    }

    //未更新大包
    public function unupdate_bigbag(Request $request)
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $bigBagID = $request->input('bigBagID', '');
        $goods_attr = $request->input('goods_attr', '');
        $des_country_id = $request->input('des_country_id', '');
        $node_id = $request->input('node_id', '');

        $query = BigBagModel::query()->with(['customer', 'destination', 'system_node', 'platform_node']);

        if($bigBagID){
            $query->where('bigBagID', $bigBagID);
        }

        if($goods_attr){
            $query->where('cargoType', $goods_attr);
        }

        if($des_country_id){
            $query->where('des_country_id', $des_country_id);
        }

        if($node_id){
            $query->where('system_node_id', $node_id);
        }

        $query->whereColumn('system_node_id', '<>', 'platform_node_id');

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item){
            $res = [
                'id' => $item->id,
                'customer_name' => $item->customer->name,
                'bigBagID' => $item->bigBagID,
                'des_country_name' => $item->toLocation,
                'goods_attr' => OrderModel::GOODS_ATTR_MAPS[$item->cargoType] ?? '',
                'system_node' => $item->system_node->name_cn ?? '',
                'platform_node' => $item->platform_node->name_cn ?? '',
            ];
            return $res;
        });


        return $this->success(['list' => $list, 'total' => $total]);

    }

    /**
     * Notes:更新平台节点
     * User: xiangzi
     * Date: 2024/11/1
     * Time: 上午10:39
     * @return \Illuminate\Http\JsonResponse
     */
    public function update_platform_node(Request $request)
    {
        $auth = $request->get('auth');

        $ids = $request->input('id',  []);

        $check = BigBagModel::whereIn('id', $ids)->whereColumn('system_node_id', '<>', 'platform_node_id')->count();

        if($check != count($ids)){
            return $this->error('请选择未更新的数据');
        }

        DB::beginTransaction();
        try{
            foreach ($ids as $id){
                $find = BigBagModel::find($id);

                if(!$find){
                    return $this->error('大包不存在');
                }

                $node = NodeModel::find($find['system_node_id']);
                $api = ApiModel::find($node->api_id);

                if(!$api){
                    return $this->error('节点接口不存在');
                }

                $record = new NodeRecordModel();
                $record->type = 2;
                $record->customer_id = $find->customer_id;
                $record->node_id = $find->system_node_id;
                $record->bigbag_id = $find->id;
                $record->bigBagID = $find->bigBagID;
                $record->datetime = date('Y-m-d H:i:s');
                $record->record_type = 1;
                $record->created_admin_id = $auth->id;

                $call_back = new CallBack();

                $res = $call_back->common_bigbag_callback($find->id, $api->nodeCode, $api->activityCode);

                if(!$res['result']){
                    $record->status = 0;
                    $record->save();
                    DB::commit();
                    return $this->error($res['msg']);
                }

                $find->platform_node_id = $find['system_node_id'];
                $find->save();
                $record->save();


                //更新小包
                \App\Models\lazada\OrderModel::where('bigBagID',$find->bigBagID)->update(['platform_node_id'=>$find['system_node_id']]);

            }

            DB::commit();

        }catch (AppException $e){
            DB::rollBack();
            return $this->error($e->getMessage());
        }

        return $this->success();
    }

    /**
     * Notes:更新平台节点
     * User: xiangzi
     * Date: 2024/11/1
     * Time: 上午10:39
     * @return \Illuminate\Http\JsonResponse
     */
    public function update_platform_node_old(Request $request)
    {
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'id' => 'required|array',
            'node_id' => 'required',
            'datetime' => 'required',
        ], [
            'id.required' => 'id不能为空',
            'node_id.required' => '请选择节点',
            'datetime.required' => '请填写时间',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $ids = $params['id'];

        $node = NodeModel::find($params['node_id']);
        $api = ApiModel::find($node->api_id);

        DB::beginTransaction();
        try{
            foreach ($ids as $id){
                $find = BigBagModel::find($id);

                if(!$find){
                    return $this->error('大包不存在');
                }

                $find->platform_node_id = $params['node_id'];
                $find->save();

                $record = new NodeRecordModel();
                $record->type = 2;
                $record->customer_id = $find->customer_id;
                $record->node_id = $params['node_id'];
                $record->bigbag_id = $find->id;
                $record->bigBagID = $find->bigBagID;
                $record->datetime = $params['datetime'];
                $record->record_type = 1;
                $record->created_admin_id = $auth->id;
                $record->save();

                $call_back = new CallBack();

                $res = $call_back->common_bigbag_callback($find->id, $params['node_id'], $params['datetime'], $api->nodeCode, $api->activityCode);

                if(!$res['result']){
                    $record->status = 0;
                    $record->save();

                    DB::rollBack();
                    return $this->error($res['msg']);
                }

            }

            DB::commit();

        }catch (AppException $e){
            DB::rollBack();
            return $this->error($e->getMessage());
        }

        return $this->success();
    }

    /**
     * Notes:详情
     * User: xiangzi
     * Date: 2024/11/1
     * Time: 上午10:24
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        $id = $request->input('id', 0);

        $find = BigBagModel::find($id);
        if(!$find){
            return $this->error('大包不存在');
        }

        $return = [
            'id' => $find->id,
            'bigBagID' => $find->bigBagID,
            'parcelQty' => $find->parcelQty,
            'trackingNumber' => $find->trackingNumber,
            'bigBagWeight' => $find->bigBagWeight,
            'weightUnit' => $find->weightUnit,
            'bigBagVolume' => $find->bigBagVolume,
            'bigBagLength' => $find->bigBagLength,
            'bigBagWidth' => $find->bigBagWidth,
            'bigBagHeight' => $find->bigBagHeight,
            'dimensionUnit' => $find->dimensionUnit,
            'cargoType' => $find->cargoType,
            'fromLocation' => $find->fromLocation,
            'toLocation' => $find->toLocation,
            'parcelList' => json_decode($find->parcelList, true),
        ];

        return  $this->success($return);
    }

    //报文
    public function data(Request $request)
    {
        $id = $request->input('id', 0);

        $find = BigBagModel::find($id);
        if(!$find){
            return $this->error('大包不存在');
        }

        if($find->data){
            $data = json_decode($find->data, true);
            unset($data['s']);
            $data = json_encode($data);
        }else{
            $data = '';
        }

//        $log = LogModel::find($find->log_id);
//
//        $data = '';
//        if($log){
//            $data = json_decode($log->data, true);
//            unset($data['s']);
//            $data = json_encode($data);
//        }

        return $this->success($data);
    }


    public function get_node_list(Request $request)
    {
        $id = $request->input('id', 0);

        $find = BigBagModel::find($id);
        if(!$find){
            return $this->error('大包不存在');
        }
        $record = NodeRecordModel::where('bigbag_id', $id)->where('type', 2)->orderBy('id', 'desc')->get();

        $list = $record->map(function ($item) {
            return [
                'id' => $item->id,
                'node_name' => $item->node->name_cn,
                'status' => $item->status == 1 ? '成功' : '失败',
                'datetime' => $item->datetime,
                'created_at' => $item->created_at->toDateTimeString()
            ];
        });

        return $this->success($list);
    }

    public function export(Request $request)
    {
//        $bigBagID = $request->input('bigBagID', '');
//        $bigBagType = $request->input('bigBagType', '');
//        $des_country_id = $request->input('des_country_id', '');
//        $warehouse_id = $request->input('warehouse_id', '');
//        $is_error = $request->input('is_error', '');
//        $is_lack = $request->input('is_lack', '');
//        $system_node_id = $request->input('node_id', '');
//        $is_update = $request->input('is_update', '');
//        $customer_id = $request->input('customer_id', '');
//        $create_start = $request->input('startTime', '');
//        $create_end = $request->input('endTime', '');

        $ids = $request->input('ids', []);

        if(!$ids){
            return $this->error('请选择要导出的数据');
        }

        $query = BigBagModel::with(['customer'])->whereIn('id', $ids);


        $items = $query
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {

            $res = [
                'customer_name' => $item->customer->name ?? '',
                'bigBagID' => $item->bigBagID,
                'des_country_name' => $item->toLocation,
                'goods_attr' => $item->cargoType == 1 ? '普货' : '敏货',
                'grossWeight' => $item->bigBagWeight . $item->weightUnit,
                'parcelQty' => $item->parcelQty
            ];
            return $res;
        });

        return Excel::download(new BigBagExport(collect($list)), 'lazada大包数据.xlsx');
    }


    public function manifest(Request $request)
    {
        $big_ids = $request->input('ids', []);

        $data = [];
        foreach ($big_ids as $big_id){
            $big = BigBagModel::find($big_id);

            if(!$big){
                return $this->error('大包不存在');
            }

            $transportNo = '';
            $sn =  '';
            if($big->take_order_id){
                $take_order = TakeOrderModel::find($big->take_order_id);
                if($take_order){
                    $sn = $take_order->sn;
                    $flight = $take_order->flight;
                    if($flight){
                        $item = $flight->items->first();
                        $transportNo = $item->air_line->flight_number ??  '';
                    }
                }
            }

            $orders = \App\Models\lazada\OrderModel::where('bigBagID', $big->bigBagID)
                ->select('orderCode', 'receiver', 'sender', 'trackingNumber', 'bigBagID', 'fromLocation', 'toLocation', 'parcel', 'features')
                ->get();

            $list = $orders->map(function($order) use ($transportNo, $sn) {
                $receiver = json_decode($order->receiver,  true);
                $receiver_address = ''; $receiver_detailAddress = '';
                if($receiver['address']){
                    $receiver_detailAddress = $receiver['address']['detailAddress'] ??  '';
                    if(isset($receiver['address']['detailAddress'])){
                        unset($receiver['address']['detailAddress']);
                    }
                    $receiver_address = implode(' ', $receiver['address']);
                }

                $sender = json_decode($order->sender,  true);
                $sender_address = ''; $sender_detailAddress = '';
                if($sender['address']){
                    $sender_detailAddress = $sender['address']['detailAddress'] ??  '';
                    if(isset($sender['address']['detailAddress'])){
                        unset($sender['address']['detailAddress']);
                    }
                    $sender_address = implode(' ', $sender['address']);
                }

                $base_data = [
                    'orderCode' => $order->orderCode,
                    'trackingNumber' => $order->trackingNumber,
                    'bigBagID' => $order->bigBagID,
                    'transportNo' => $transportNo,
                    'mawbNumber' => $sn,
                    'fromLocation' => $order->fromLocation,
                    'toLocation' => $order->toLocation,
                    'receiver_name' => $receiver['name'] ??  '',
                    'receiver_phone' => $receiver['phone'] ??  '',
                    'receiver_mobile' => $receiver['mobile'] ??  '',
                    'receiver_email' => $receiver['email'] ??  '',
                    'receiver_zipCode' => $receiver['zipCode'] ??  '',
                    'receiver_address' => $receiver_address,
                    'receiver_detailAddress' => $receiver_detailAddress,
                    'sender_name' => $sender['name'] ??  '',
                    'sender_phone' => $sender['phone'] ??  '',
                    'sender_mobile' => $sender['mobile'] ??  '',
                    'sender_email' => $sender['email'] ??  '',
                    'sender_zipCode' => $sender['zipCode'] ??  '',
                    'sender_address' => $sender_address,
                    'sender_detailAddress' => $sender_detailAddress,
                ];

                $parcel = json_decode($order->parcel,  true);
                $goodsList = $parcel['goodsList'] ?? [];

                $features = $order->features;
                $taxId = '';
                if($features){
                    $features = json_decode($features,  true);
                    $taxId = $features['taxId'] ?? '';
                }

                $re = [];
                foreach ($goodsList as $item){
                    $lvgPaid = $item['extension']['lvgPaid'] ?? '';
                    $res = $base_data;
                    $res['weight'] = $parcel['weight'] ?? '';
                    $res['weightUnit'] = $parcel['weightUnit'] ?? '';
                    $res['name'] = $item['name'] ??  '';
                    $res['localName'] = $item['localName'] ??  '';
                    $res['categoryName'] = $item['categoryName'] ??  '';
                    $res['price'] = $item['price'] ?? '';
                    $res['itemPrice'] = $item['itemPrice'] ?? '';
                    $res['priceUnit'] = $item['priceUnit'] ??  '';
                    $res['priceCurrency'] = $item['priceCurrency'] ?? '';
                    $res['quantity'] = $item['quantity'] ?? '';
                    $res['hsCode'] = $item['hsCode'] ??  '';
                    $res['declaredValue'] = $item['declaredValue'] ?? '';
                    $res['lvgPaid'] = $lvgPaid;
                    $res['taxId'] = $taxId;

                    $re[] = $res;
                }

                return $re;
            });

            if($list->isNotEmpty()){
                $list = $list->toArray();
                foreach ($list as $value){
                    $data = array_merge($data, $value);
                }
            }


        }

        return Excel::download(new BigBagManifestExport(collect($data)), 'manifest.xlsx');




    }


    public function manifest2(Request $request)
    {
        $big_ids = $request->input('ids', []);

        $data = [];
        foreach ($big_ids as $big_id){
            $big = BigBagModel::find($big_id);

            if(!$big){
                return $this->error('大包不存在');
            }

            $transportNo = '';
            $sn =  '';
            if($big->take_order_id){
                $take_order = TakeOrderModel::find($big->take_order_id);
                if($take_order){
                    $sn = $take_order->sn;
                    $flight = $take_order->flight;
                    if($flight){
                        $item = $flight->items->first();
                        $transportNo = $item->air_line->flight_number ??  '';
                    }
                }
            }

            $orders = \App\Models\lazada\OrderModel::where('bigBagID', $big->bigBagID)
                ->select('orderCode', 'receiver', 'sender', 'trackingNumber', 'bigBagID', 'fromLocation', 'toLocation', 'parcel', 'features')
                ->get();

            $list = $orders->map(function($order) use ($transportNo, $sn) {
                $receiver = json_decode($order->receiver,  true);
                $receiver_address = ''; $receiver_detailAddress = '';
                if($receiver['address']){
                    $receiver_detailAddress = $receiver['address']['detailAddress'] ??  '';
                    if(isset($receiver['address']['detailAddress'])){
                        unset($receiver['address']['detailAddress']);
                    }
                    $receiver_address = implode(' ', $receiver['address']);
                }

                $sender = json_decode($order->sender,  true);
                $sender_address = ''; $sender_detailAddress = '';
                if($sender['address']){
                    $sender_detailAddress = $sender['address']['detailAddress'] ??  '';
                    if(isset($sender['address']['detailAddress'])){
                        unset($sender['address']['detailAddress']);
                    }
                    $sender_address = implode(' ', $sender['address']);
                }

                $base_data = [
                    'orderCode' => $order->orderCode,
                    'trackingNumber' => $order->trackingNumber,
                    'bigBagID' => $order->bigBagID,
                    'transportNo' => $transportNo,
                    'mawbNumber' => $sn,
                    'fromLocation' => $order->fromLocation,
                    'toLocation' => $order->toLocation,
                    'receiver_name' => $receiver['name'] ??  '',
                    'receiver_phone' => $receiver['phone'] ??  '',
                    'receiver_mobile' => $receiver['mobile'] ??  '',
                    'receiver_email' => $receiver['email'] ??  '',
                    'receiver_zipCode' => $receiver['zipCode'] ??  '',
                    'receiver_address' => $receiver_address,
                    'receiver_detailAddress' => $receiver_detailAddress,
                    'sender_name' => $sender['name'] ??  '',
                    'sender_phone' => $sender['phone'] ??  '',
                    'sender_mobile' => $sender['mobile'] ??  '',
                    'sender_email' => $sender['email'] ??  '',
                    'sender_zipCode' => $sender['zipCode'] ??  '',
                    'sender_address' => $sender_address,
                    'sender_detailAddress' => $sender_detailAddress,
                ];

                $parcel = json_decode($order->parcel,  true);
                $goodsList = $parcel['goodsList'] ?? [];

                $features = $order->features;
                $taxId = '';
                if($features){
                    $features = json_decode($features,  true);
                    $taxId = $features['taxId'] ?? '';
                }

                $re = [];
                $goods_num = count($goodsList);
                foreach ($goodsList as $item){
                    $lvgPaid = $item['extension']['lvgPaid'] ?? '';
                    $res = $base_data;
                    $res['weight'] = round($parcel['weight']/$goods_num, 2) ?? '';
                    $res['weightUnit'] = $parcel['weightUnit'] ?? '';
                    $res['name'] = $item['name'] ??  '';
                    $res['localName'] = $item['localName'] ??  '';
                    $res['categoryName'] = $item['categoryName'] ??  '';
                    $res['price'] = $item['price'] ?? '';
                    $res['itemPrice'] = $item['itemPrice'] ?? '';
                    $res['priceUnit'] = $item['priceUnit'] ??  '';
                    $res['priceCurrency'] = $item['priceCurrency'] ?? '';
                    $res['quantity'] = $item['quantity'] ?? '';
                    $res['hsCode'] = $item['hsCode'] ??  '';
                    $res['declaredValue'] = $item['declaredValue'] ?? '';
                    $res['lvgPaid'] = $lvgPaid;
                    $res['taxId'] = $taxId;

                    $re[] = $res;
                }

                return $re;
            });

            if($list->isNotEmpty()){
                $list = $list->toArray();
                foreach ($list as $value){
                    $data = array_merge($data, $value);
                }
            }


        }

        return Excel::download(new BigBagManifestExport(collect($data)), 'manifest.xlsx');




    }

}
